package com.ruoyi.fitsculpt.controller;

import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fitsculpt.domain.EasyfitUsers;
import com.ruoyi.fitsculpt.service.IEasyfitUsersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.fitsculpt.utils.TokenUtils;
import com.ruoyi.fitsculpt.domain.EasyfitUserGoals;
import com.ruoyi.fitsculpt.service.IEasyfitUserGoalsService;
import com.ruoyi.fitsculpt.domain.EasyfitExercisePlans;
import com.ruoyi.fitsculpt.service.IEasyfitExercisePlansService;
import com.ruoyi.fitsculpt.domain.EasyfitDietPlans;
import com.ruoyi.fitsculpt.service.IEasyfitDietPlansService;
import com.ruoyi.fitsculpt.domain.EasyfitBodyStats;
import com.ruoyi.fitsculpt.service.IEasyfitBodyStatsService;

/**
 * 健身小程序用户Controller
 *
 * @author metaee
 * @date 2024-02-24
 */
@Api(tags = "健身小程序用户管理")
@RestController
@RequestMapping("/fitsculpt/users")
public class EasyfitUsersController extends BaseController {
    @Autowired
    private IEasyfitUsersService easyfitUsersService;

    @Autowired
    private IEasyfitUserGoalsService easyfitUserGoalsService;

    @Autowired
    private IEasyfitExercisePlansService easyfitExercisePlansService;

    @Autowired
    private IEasyfitDietPlansService easyfitDietPlansService;

    @Autowired
    private IEasyfitBodyStatsService easyfitBodyStatsService;

    /**
     * 查询健身小程序用户列表
     */
    @ApiOperation("查询健身小程序用户列表")
    @GetMapping("/list")
    public TableDataInfo list(EasyfitUsers easyfitUsers) {
        startPage();
        List<EasyfitUsers> list = easyfitUsersService.selectEasyfitUsersList(easyfitUsers);
        return getDataTable(list);
    }

    /**
     * 导出健身小程序用户列表
     */
    @ApiOperation("导出健身小程序用户列表")
    @Log(title = "健身小程序用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EasyfitUsers easyfitUsers) {
        List<EasyfitUsers> list = easyfitUsersService.selectEasyfitUsersList(easyfitUsers);
        ExcelUtil<EasyfitUsers> util = new ExcelUtil<EasyfitUsers>(EasyfitUsers.class);
        util.exportExcel(response, list, "健身小程序用户数据");
    }

    /**
     * 获取用户基础信息详细信息
     */
    @ApiOperation("获取用户基础信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(id);
        if (user != null) {
            // 查询用户的目标，按创建时间倒序
            EasyfitUserGoals queryGoal = new EasyfitUserGoals();
            queryGoal.setUserId(id);
            List<EasyfitUserGoals> goals = easyfitUserGoalsService.selectEasyfitUserGoalsList(queryGoal);
            goals.sort((a, b) -> b.getCreateTime().compareTo(a.getCreateTime()));
            user.setGoals(goals);

            // 查询用户的运动计划，按创建时间倒序
            EasyfitExercisePlans queryExercisePlan = new EasyfitExercisePlans();
            queryExercisePlan.setUserId(id);
            List<EasyfitExercisePlans> exercisePlans = easyfitExercisePlansService.selectEasyfitExercisePlansList(queryExercisePlan);
            exercisePlans.sort((a, b) -> b.getCreateTime().compareTo(a.getCreateTime()));
            user.setExercisePlans(exercisePlans);

            // 查询用户的饮食计划，按创建时间倒序
            EasyfitDietPlans queryDietPlan = new EasyfitDietPlans();
            queryDietPlan.setUserId(id);
            List<EasyfitDietPlans> dietPlans = easyfitDietPlansService.selectEasyfitDietPlansList(queryDietPlan);
            dietPlans.sort((a, b) -> b.getCreateTime().compareTo(a.getCreateTime()));
            user.setDietPlans(dietPlans);

            // 查询用户的身体数据记录，按记录日期倒序
            EasyfitBodyStats queryBodyStats = new EasyfitBodyStats();
            queryBodyStats.setUserId(id);
            List<EasyfitBodyStats> bodyStats = easyfitBodyStatsService.selectEasyfitBodyStatsList(queryBodyStats);
            bodyStats.sort((a, b) -> b.getRecordDate().compareTo(a.getRecordDate()));
            user.setBodyStats(bodyStats);

            // 设置最新的身体数据
            if (!bodyStats.isEmpty()) {
                user.setLatestBodyStats(bodyStats.get(0));
            }

            // 设置当前进行中的目标
            goals.stream()
                .filter(goal -> goal.getStatus() != null && goal.getStatus() == 1L) // 1:进行中
                .findFirst()
                .ifPresent(user::setCurrentGoal);

            // 设置当前进行中的运动计划
            exercisePlans.stream()
                .filter(plan -> plan.getStatus() != null && plan.getStatus() == 1L) // 1:进行中
                .findFirst()
                .ifPresent(user::setCurrentExercisePlan);

            // 设置当前进行中的饮食计划
            dietPlans.stream()
                .filter(plan -> plan.getStatus() != null && plan.getStatus() == 1L) // 1:进行中
                .findFirst()
                .ifPresent(user::setCurrentDietPlan);
        }
        return success(user);
    }

    /**
     * 新增健身小程序用户
     */
    @ApiOperation("新增健身小程序用户")
    @Log(title = "健身小程序用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EasyfitUsers easyfitUsers) {
        return toAjax(easyfitUsersService.insertEasyfitUsers(easyfitUsers));
    }

    /**
     * 修改健身小程序用户
     */
    @ApiOperation("修改健身小程序用户")
    @Log(title = "健身小程序用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EasyfitUsers easyfitUsers) {
        return toAjax(easyfitUsersService.updateEasyfitUsers(easyfitUsers));
    }

    /**
     * 删除健身小程序用户
     */
    @ApiOperation("删除健身小程序用户")
    @Log(title = "健身小程序用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@ApiParam(value = "健身小程序用户ID数组", required = true) @PathVariable Long[] userIds) {
        return toAjax(easyfitUsersService.deleteEasyfitUsersByUserIds(userIds));
    }

}
